var href = window.location.href;
month = href.substring(href.indexOf('month=')+6,href.length);
$(function() {
    var taskTable = null;
    var opts = {
        targetId: "taskTable",
        templateId: "tabletask",
        settings: {
            searchParam: {
            	'month':month
            },
            url: DI.queryTmpTask,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            }
        }
        
    };
    //实例化数据表，获取主推任务列表信息
    taskTable = SwiftUI.swiftTable.getInstance(opts);

    var mainCtrl = avalon.define({
        $id: "mainController",
        monthTime:getMonth(month),
        importSecond:function(){
        	$('#upladFile').click();
        }
    });
    $('#upladFile').unbind('change').bind('change',function(){
    	uploadFile();
    });
    
    var popCtrl = avalon.define({
        $id: "popController",
        delData: function() {
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "taskNo": mainCtrl.deltaskNo,
                    "taskTopic": mainCtrl.deltaskTopic
                },
                url: DI.deleteTask,
                success: function(data) {
                    $("#popup_del").hide();
                    if (data.retFlag == 1) {
                        PLATFORM.showTips("success", "删除成功");
                        taskTable.searchParam.pageNo = "1";
                        taskTable.refresh();
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        }
    });

    avalon.scan();
});

avalon.filters.filterRate = function(rate,arg1,arg2){
	return jsMul(rate , 100) + '%';
}
function jsMul(arg1,arg2){
    var m=0,s1=arg1.toString(),s2=arg2.toString();
    try{m+=s1.split(".")[1].length}catch(e){}
    try{m+=s2.split(".")[1].length}catch(e){}
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}

function getMonth(month){
	var m = month.substring(0,4)+'年'+month.substring(4,6)+'月';
	return m;
}

function uploadFile (){
 	var inputFile = '#upladFile';
    if(!$(inputFile).val()){
        return;
    }
    var data = new FormData();
    data.append('secondExcel', $(inputFile)[0].files[0]);
    data.append('month', month);
    $('.loadData').show();
    $.ajax({
        url: DI.importSecondExcel,
        type: 'POST',
        data: data,
        dataType:'json',
        processData: false,
        contentType: false
    }).done(function (data) {
    	if(data.retFlag == 1){
    		PLATFORM.showTips("success","导入成功");
    		window.location.href = 'hqTaskList.html';
    	}else{
    		var message = data.errorMessage;
    		if(message){
    			$('#error').show().html("1. "+message);
    		}else{
    			var errorList = data.data;
    			var html = '';
    			for(var i = 0;i<errorList.length;i++){
    				html += (i+1)+'. '+errorList[i]+'<br/>';
    			}
    			$('#error').show().html(html);
    		}
    	}

    }).fail(function(){
        PLATFORM.showTips("error","导入失败");
    }).always (function(){
    	$('#upladFile').val('');
    	 $('.loadData').hide();
    });
}